package com.dongway.netcondition.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.dongway.core.service.BaseService;
import com.dongway.core.utils.JdbcUtils;
import com.dongway.netcondition.criteria.BaseCriteria;
import com.dongway.netcondition.dto.AutoCompleteItem;
import com.dongway.netcondition.model.RegionBranch;

@Service
public class RegionBranchService extends BaseService {

    public List<RegionBranch> query(BaseCriteria criteria) {
        StringBuilder sql =
                new StringBuilder("select * from " + JdbcUtils.getTableName(RegionBranch.class));
        Map<String, Object> map = new HashMap<String, Object>();
        if (null != criteria.getId()) {
            JdbcUtils.appendWhereOrAnd(sql);
            sql.append("id=:id");
            map.put("id", criteria.getId());
        }
        if (StringUtils.isNotBlank(criteria.getName())) {
            JdbcUtils.appendWhereOrAnd(sql);
            sql.append("name=:name");
            map.put("name", criteria.getName());
        }
        if (StringUtils.isNotBlank(criteria.getFuzzyWord())) {
            JdbcUtils.appendWhereOrAnd(sql);
            sql.append("name like %:name%");
            map.put("name", criteria.getFuzzyWord());
        }
        return getDao().query(RegionBranch.class, sql.toString(), map);
    }


    public List<AutoCompleteItem> all() {
        String sql = "select id, name from t_region_branch";
        return getDao().query(AutoCompleteItem.class, sql);
    }


    public void update(RegionBranch branch) {
        if (null != branch.getId()) {
            getDao().updateIgnoreNull(branch);
        } else {
            getDao().save(branch);
        }
    }
}
